package com.Houserentsys;

import com.Houserentsys.Utility.Utility;
import com.Houserentsys.domain.House;
import com.Houserentsys.service.HouseService;

public class HouseView {

    boolean loop = true; //决定程序是否终止
    char key = ' ';//主菜单选择
    HouseService houseService = new HouseService(2);//分配十个大小空间

    public void updateHouse() {

        System.out.println("----------修改客户----------");
        System.out.println("请选择待修改房屋编号(-1退出)");
        int updateId = Utility.readInt();

        if (updateId == -1) {
            return;
        }

        if(houseService.findId(updateId) != null) {

            System.out.println("姓名"+houseService.findId(updateId).getName());
            String name = Utility.readString(8,"");
            if (!(name.equals(""))) {
                houseService.findId(updateId).setName(name);
            }

            System.out.println("电话"+houseService.findId(updateId).getPhone());
            String phone = Utility.readString(13,"");
            if (!(phone.equals(""))) {
                houseService.findId(updateId).setPhone(phone);
            }

            System.out.println("地址"+houseService.findId(updateId).getAddress());
            String address = Utility.readString(8,"");
            if (!(address.equals(""))) {
                houseService.findId(updateId).setAddress(address);
            }

            System.out.println("租金"+houseService.findId(updateId).getRent());
            String rent = Utility.readString(8,"");
            if (!(rent.equals(""))) {
                houseService.findId(updateId).setRent(rent);
            }

            System.out.println("状态"+houseService.findId(updateId).getStatus());
            String status = Utility.readString(8,"");
            if (!(status.equals(""))) {
                houseService.findId(updateId).setStatus(status);
            }



        } else {
            System.out.println("----------你所输入的房屋信息不存在----------");
        }

    }

    public  void selHouse(){
        System.out.println("----------查找房屋----------");
        System.out.println("请输入你要查找的id:");

        int id = Utility.readInt();

        House house = houseService.findId(id);
        if(house != null){
            System.out.println(house);
        } else {
            System.out.println("----------未查找到第"+id+"个房屋----------");

        }

    }


    public void delHouse() {

        System.out.println("----------删除房屋----------");
        System.out.println("请选择待删除的房屋编号");
        int delId = Utility.readInt();

        if (delId == -1) {
            System.out.println("----------放弃删除房屋----------");
            return;
        }

        System.out.println("是否删除，请小心选择(Y/N)");
        char choice = Utility.readChar();
        if (choice == 'Y') {

            if(houseService.HouseDel(delId)) {
                System.out.println("----------删除房屋成功----------");
            } else {
                System.out.println("----------删除房屋失败----------");
            }
        } else {
            System.out.println("放弃删除房屋信息");
        }

    }

    public void addHouse() {

        System.out.println("----------添加房屋----------");
        System.out.print("姓名");
        String name = Utility.readString(8);
        System.out.print("电话");
        String phone = Utility.readString(8);
        System.out.print("地址");
        String address = Utility.readString(8);
        System.out.print("月租");
        String rent = Utility.readString(8);
        System.out.print("状态(未出租/已出租)");
        String status = Utility.readString(8);

        House newHouse = new House(0,name,phone,address,rent,status);

        if(houseService.add(newHouse)){
            System.out.println(("----------添加房屋成功----------"));
        } else {
            System.out.println(("----------添加房屋失败----------"));
        }

    }


    public void listHouses() {
        System.out.println("----------房屋列表----------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();//获取所有房屋信息

        for(int i = 0; i < houses.length; i++) {
            if(houses[i] == null){
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("----------房屋信息显示完毕----------");
    }

    public void mainMenu(){

        do{
            System.out.println("----------房屋出租系统----------");
            System.out.println("\t\t\t1.新增房源");
            System.out.println("\t\t\t2.查找房源");
            System.out.println("\t\t\t3.删除房源");
            System.out.println("\t\t\t4.修改房屋信息");
            System.out.println("\t\t\t5.房屋列表");
            System.out.println("\t\t\t6.退     出");
            System.out.println("请输入(1-6):");

            key=Utility.readChar();// 使用Utility工具包

            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    selHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    while(loop) {
                        char root = Utility.readConfirmSelection();
                        if(root == 'Y'){
                            loop = false;
                        } else if(root == 'N'){
                            loop = true;
                            break;
                        } else {
                            System.out.println("选择错误，请重新输入~");
                        }
                    }

                    break;
            }
        }while(loop);

    }

}

